local ffi = require('ffi');
ffi.cdef[[
    typedef short wchar_t;
    typedef struct TTF_Font TTF_Font;
    void TTF_ByteSwappedUNICODE(int swapped);
    void TTF_CloseFont(TTF_Font *font);
    int TTF_FontAscent(const TTF_Font *font);
    int TTF_FontDescent(const TTF_Font *font);
    char* TTF_FontFaceFamilyName(const TTF_Font *font);
    int TTF_FontFaceIsFixedWidth(const TTF_Font *font);
    char* TTF_FontFaceStyleName(const TTF_Font *font);
    long int TTF_FontFaces(const TTF_Font *font);
    int TTF_FontHeight(const TTF_Font *font);
    int TTF_FontLineSkip(const TTF_Font *font);
    int TTF_GetFontHinting(const TTF_Font *font);
    int TTF_GetFontKerning(const TTF_Font *font);
    int TTF_GetFontKerningSize(TTF_Font *font,int prev_index,int index);
    int TTF_GetFontKerningSizeGlyphs(TTF_Font *font,Uint16 previous_ch,Uint16 ch);
    int TTF_GetFontOutline(const TTF_Font *font);
    int TTF_GetFontStyle(const TTF_Font *font);
    int TTF_GlyphIsProvided(const TTF_Font *font,Uint16 ch);
    int TTF_GlyphMetrics(TTF_Font *font,Uint16 ch,int *minx,int *maxx,int *miny,int *maxy,int *advance);
    int TTF_Init();
    struct SDL_version* TTF_Linked_Version();
    struct TTF_Font* TTF_OpenFont(const char *file,int ptsize);
    struct TTF_Font* TTF_OpenFontIndex(const char *file,int ptsize,long int index);
    struct TTF_Font* TTF_OpenFontIndexRW(SDL_RWops *src,int freesrc,int ptsize,long int index);
    struct TTF_Font* TTF_OpenFontRW(SDL_RWops *src,int freesrc,int ptsize);
    void TTF_Quit();
    struct SDL_Surface* TTF_RenderGlyph_Blended(TTF_Font *font,Uint16 ch,SDL_Color fg);
    struct SDL_Surface* TTF_RenderGlyph_Shaded(TTF_Font *font,Uint16 ch,SDL_Color fg,SDL_Color bg);
    struct SDL_Surface* TTF_RenderGlyph_Solid(TTF_Font *font,Uint16 ch,SDL_Color fg);
    struct SDL_Surface* TTF_RenderText_Blended(TTF_Font *font,const char *text,SDL_Color fg);
    struct SDL_Surface* TTF_RenderText_Blended_Wrapped(TTF_Font *font,const char *text,SDL_Color fg,Uint32 wrapLength);
    struct SDL_Surface* TTF_RenderText_Shaded(TTF_Font *font,const char *text,SDL_Color fg,SDL_Color bg);
    struct SDL_Surface* TTF_RenderText_Solid(TTF_Font *font,const char *text,SDL_Color fg);
    struct SDL_Surface* TTF_RenderUNICODE_Blended(TTF_Font *font,const Uint16 *text,SDL_Color fg);
    struct SDL_Surface* TTF_RenderUNICODE_Blended_Wrapped(TTF_Font *font,const Uint16 *text,SDL_Color fg,Uint32 wrapLength);
    struct SDL_Surface* TTF_RenderUNICODE_Shaded(TTF_Font *font,const Uint16 *text,SDL_Color fg,SDL_Color bg);
    struct SDL_Surface* TTF_RenderUNICODE_Solid(TTF_Font *font,const Uint16 *text,SDL_Color fg);
    struct SDL_Surface* TTF_RenderUTF8_Blended(TTF_Font *font,const char *text,SDL_Color fg);
    struct SDL_Surface* TTF_RenderUTF8_Blended_Wrapped(TTF_Font *font,const char *text,SDL_Color fg,Uint32 wrapLength);
    struct SDL_Surface* TTF_RenderUTF8_Shaded(TTF_Font *font,const char *text,SDL_Color fg,SDL_Color bg);
    struct SDL_Surface* TTF_RenderUTF8_Solid(TTF_Font *font,const char *text,SDL_Color fg);
    void TTF_SetFontHinting(TTF_Font *font,int hinting);
    void TTF_SetFontKerning(TTF_Font *font,int allowed);
    void TTF_SetFontOutline(TTF_Font *font,int outline);
    void TTF_SetFontStyle(TTF_Font *font,int style);
    int TTF_SizeText(TTF_Font *font,const char *text,int *w,int *h);
    int TTF_SizeUNICODE(TTF_Font *font,const Uint16 *text,int *w,int *h);
    int TTF_SizeUTF8(TTF_Font *font,const char *text,int *w,int *h);
    int TTF_WasInit();    
]]

local ttf;
local SDL2 = ffi.load('SDL2');
if(ANDROID)then
    ttf = loadso('libSDL2_ttf');
else
    ttf = ffi.load('SDL2_ttf');
end
ttf.TTF_Init();
local default = {
fontSize = 24,
font = nil,
style = 'solid',
bg = 0xffffffff,
fontPath = [[C:\Windows\Fonts\SIMSUN.ttf]],
}

if(ANDROID)then
    default.fontPath = '/system/fonts/Miui-Bold.ttf';
    default.fontSize = 60;
end

local tmpcolor = ffi.new('SDL_Color[5]');
local tmpcolorint = ffi.cast('uint32_t*',tmpcolor);

local function render_Blended(font,text,fontColor)
    tmpcolorint[0] = fontColor;
    return ttf.TTF_RenderUTF8_Blended(font.font,text,tmpcolor[0]);
end

local function render_Shaded(font,text,fontColor)
    tmpcolorint[0] = fontColor;
    tmpcolorint[1] = font.bg;
    return ttf.TTF_RenderUTF8_Shaded(font.font,text,tmpcolor[0],tmpcolor[1]);
end

local function render_Solid(font,text,fontColor)
    tmpcolorint[0] = fontColor;
    return ttf.TTF_RenderUTF8_Solid(font.font,text,tmpcolor[0]);
end


local fontStyle = {
    ['blended'] = render_Blended,
    ['shaded'] = render_Shaded,
    ['solid'] = render_Solid,
}

local TextDraw = {};

local function openFont(path,fsize)
    local f = ttf.TTF_OpenFont(path,fsize);
    return ffi.gc(f,ttf.TTF_CloseFont);
end

local function setDefault(t,name,v)
    if(t[name]==nil)then
        t[name] = v;
    end
end

function Text_CreateSurface(text,font)
    if(text==nil or #text==0)then return nil; end;
    if(font==nil)then
        font = default;
    else
        for k,v in pairs(default)do
            --设置默认值
            setDefault(font,k,v);
        end
    end
    --初始化字体
    if(font.font==nil)then
        font.font = openFont(font.fontPath,font.fontSize);
        if(font.font==nil)then
            return nil;
        end
        assert(font.font~=nil,"字体加载错误");
    end
    local style = font.style;
    local render = fontStyle[style];
    if(render==nil)then
        render = render_Shaded;
    end
    local fontColor = font.fontColor;
    if(fontColor==nil)then
        fontColor = 0xff000000;
    end
    return ffi.gc(render(font,text,fontColor),SDL2.SDL_FreeSurface);
end

